package com.millstein.tsinglog.service.impl;

import com.millstein.tsinglog.commons.constant.SystemConstant;
import com.millstein.tsinglog.commons.enums.ResultCode;
import com.millstein.tsinglog.exception.TsinglogException;
import com.millstein.tsinglog.commons.service.BaseService;
import com.millstein.tsinglog.utils.ConcurrentDateUtils;
import com.millstein.tsinglog.utils.DateUtils;
import com.millstein.tsinglog.mapper.biz.StatisticsMapper;
import com.millstein.tsinglog.service.StatisticsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class StatisticsServiceImpl extends BaseService implements StatisticsService {

    @Autowired
    private StatisticsMapper statisticsMapper;

    @Override
    public Map<String, Object> showDataByType(Integer type) {

        String dateStr = "";
        Map<String, Object> map = null;

        if (type == null) {
            throw new TsinglogException(ResultCode.PARAM_IS_BLANK);
        } else if (type == SystemConstant.STATISTICS_TODAY) {
            try {
                dateStr = ConcurrentDateUtils.format(DateUtils.getTodayDate());
                map = this.getCountDataByDate(dateStr);
            } catch (ParseException e) {
                e.printStackTrace();
                throw new TsinglogException(ResultCode.SERVER_OCCURS_ERROR);
            }
        } else if (type == SystemConstant.STATISTICS_THIS_MONTH) {
            try {
                dateStr = ConcurrentDateUtils.format(DateUtils.getFirstDayForMonth());
                map = this.getCountDataByDate(dateStr);
            } catch (ParseException e) {
                e.printStackTrace();
                throw new TsinglogException(ResultCode.SERVER_OCCURS_ERROR);
            }
        } else if (type == SystemConstant.STATISTICS_ALL) {
            dateStr = SystemConstant.THE_TIME_ERA;
            map = this.getCountDataByDate(dateStr);
        } else {
            throw new TsinglogException(ResultCode.PARAM_IS_INVALID);
        }


        return map;
    }

    @Override
    public List<Map<String, Object>> showArticleCountSortedByCategory() {
        return statisticsMapper.showArticleCountSortedByCategory();
    }

    @Override
    public List<Map<String, Object>> showArticleCountSortedByTag() {
        return statisticsMapper.showArticleCountSortedByTag();
    }

    /**
     * 根据起始时间查询文章和评论的数量，并查询所有的分类和标签的数量
     * @param dateStr 起始时间
     * @return
     */
    private Map<String, Object> getCountDataByDate(String dateStr) {

        Map<String, Object> map = new HashMap<String, Object>();

        map.put("article", statisticsMapper.getArticleCountByDate(dateStr));
        map.put("category", statisticsMapper.getCategoryCountByDate(SystemConstant.THE_TIME_ERA));
        map.put("tag", statisticsMapper.getTagCountByDate(SystemConstant.THE_TIME_ERA));
        map.put("comment", statisticsMapper.getCommentCountByDate(dateStr));

        return map;
    }
}
